home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / BOOTRCD.ZIP;1 / BRECORD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-14  |  4.7 KB  |  161 lines

  1. //      BRECORD.CPP  ...  Boot record routines
  2.  
  3. //      Copyright 1992 by George H. Mealy
  4.  
  5. #include    "brecord.h"
  6. #include    <stdlib.h>
  7. #include    <string.h>
  8. #include    <alloc.h>
  9. #include    <dos.h>
  10. #include    <stdio.h>
  11.  
  12. brecord::brecord(char drv)          // Constructor.  Reads boot record.
  13. {
  14.     rootdict = NULL;
  15.     FAT = NULL;
  16.     FAT12 = NULL;
  17.     drive = drv&0xf - 1;
  18.     if (absread(drive, 1, 0, bjunk1))
  19.         {puts("Cannot read boot sector\n\a"); exit(-1);}
  20.     sectorsize = bbytes;
  21.     clustersize = bsect;
  22.     dictsize = bndir;
  23.     dictsects = dictsize*32/sectorsize;
  24.     nFATs = bfat;
  25.     FATsize = bfatsect;
  26.     data_origin = brsvd+nFATs*FATsize+dictsects;
  27.     if (btotal) b32total = btotal;
  28.     is16bit = b32total >= 20740;
  29.     endtest = is16bit? 0xfff8: 0xff8;
  30.     endcluster = (b32total - data_origin) / clustersize + 2;
  31.     struct dfree dfree;
  32.     getdfree(drive + 1, &dfree);
  33.     freecount = dfree.df_avail;
  34. }
  35.  
  36. brecord::~brecord()
  37. {
  38.     if (rootdict) delete rootdict;
  39.     if (FAT) delete FAT;
  40. }
  41.  
  42. /*    Print the boot record data    */
  43.  
  44. void brecord::listboot()      /* Print boot record data */
  45. {
  46.     *btail = '\0';
  47.     printf(
  48. "\33[2J\n\n\n    \33[1;4mDrive %c: boot record information\33[0m\n\n\
  49.     Vendor ID           %8s\n    Bytes per sector    %u\n\
  50.     Sectors per cluster    %u\n",
  51.     'A' + drive, bvendor, bbytes, bsect);
  52.     printf(
  53. "    # reserved sectors  %u\n    # FATs              %u\n\
  54.     # directory entries %u\n    # sectors           %lu\n",
  55.     brsvd, bfat, bndir, b32total);
  56.     printf(
  57. "    Media type          %2X\n    Sectors per FAT     %u\n\
  58.     Sectors per track   %u\n    # heads             %u\n\
  59.     # hidden sectors    %lu\n    # free clusters     %u\n\
  60.     Bits per FAT entry  %u\n\
  61.     Volume serial       %u.%u\n\
  62.     Volume label        %11s\n\n",
  63.     btype, bfatsect, btracks,
  64.     bheads, bhidden, freecount, is16bit? 16: 12,
  65.     FP_SEG(bserial), FP_OFF(bserial), bvolume);
  66. }
  67.  
  68. /*    Get the root directory    */
  69.  
  70. void brecord::getrootdict()
  71. {
  72.     unsigned start = brsvd+nFATs*FATsize, err;
  73.     if (!rootdict) rootdict = (DIR *)new char[dictsects*sectorsize];
  74.     #pragma option -w-pia
  75.     if (absread(drive, dictsects, start, rootdict))
  76.         {perror("\aBad dictionary read"); exit(-1);}
  77. }
  78.  
  79. /*    Get the file allocation table    */
  80.  
  81. void brecord::getFAT()
  82. {
  83.     FAT12 = FAT = (unsigned *)malloc((FATsize/2)*sectorsize);
  84.     absread(drive, FATsize, brsvd, FAT);    /* Read a FAT */
  85. }
  86.  
  87. int brecord::readdir(unsigned cluster)
  88. {
  89.     int i = 0;
  90.     while (cluster < endtest)
  91.     {    // Reads a subdirectory into the root directory buffer
  92.         absread(drive, clustersize, sector(cluster), &rootdict[i]);
  93.         cluster = FATentry(cluster);
  94.         i += (sectorsize/sizeof(DIR)) * clustersize;
  95.     }
  96.     rootdict[i].stem[0] = '\0';
  97.  
  98.     return i;
  99. }
  100.  
  101. int brecord::writedir(unsigned cluster)
  102. {
  103.     int i = 0;
  104.     if (! cluster)
  105.           return abswrite(drive, dictsects,
  106.             brsvd+nFATs*FATsize, rootdict);
  107.     while (1)
  108.     {
  109.         abswrite(drive, clustersize, sector(cluster), &rootdict[i]);
  110.         if (cluster >= endtest) break;
  111.         cluster = FATentry(cluster);
  112.         i += (sectorsize/sizeof(DIR))*clustersize;
  113.         }
  114.     return i;
  115. }
  116.  
  117. int brecord::clength(unsigned x)             /* Get number of clusters in file */
  118. {
  119.     unsigned n = 0;
  120.     for (; x < 0xfff8; x = FATentry(x)) n++;
  121.     return n;
  122. }
  123.  
  124.  
  125. unsigned brecord::sector(unsigned cluster)     /* Convert cluster # to sector # */
  126. {
  127.     return (cluster-2)*clustersize+data_origin;
  128. }
  129.  
  130. unsigned brecord::FATentry(unsigned c)             /* Get FAT entry for cluster */
  131. {
  132.     if (is16bit) return FAT[c];    /* 16 bit FAT entry */
  133.     else {                /* 12 bit FAT entry */
  134.         unsigned index = (3*c)>>1;
  135.         unsigned word = FAT12[index] | (FAT12[index+1]<<8);
  136.         unsigned result = (c&1? word>>4: word&0xfff);
  137.         return result;
  138.         }
  139. }
  140.  
  141. void brecord::derase()              // Quick unformat
  142. {
  143.     if (drive > 1) {puts("Cannot recycle a hard disk"); return;}
  144.     bdos(0xd, 0, 0);                            // Flush I/O
  145.     FAT = (unsigned *)new char[sectorsize];     // Allocate only one FAT sector
  146.     absread(drive, 1, brsvd, FAT);              // Read first sector of FAT
  147.     memset((char *)FAT+3, 0, sectorsize-3);     // Set most of it to zeroes
  148.     abswrite(drive, 1, brsvd, FAT);             // Write to both FATs
  149.     abswrite(drive, 1, brsvd+FATsize, FAT);
  150.     memset(FAT, 0, 3);                          // Clear the rest to zeroes
  151.     for (int i = 1; i < FATsize; i++) {
  152.         abswrite(drive, 1, brsvd+i, FAT);       // Erase both FATs
  153.         abswrite(drive, 1, brsvd+FATsize+i, FAT);
  154.     }
  155.     for (i = 0; i < dictsects; i++)
  156.         abswrite(drive, 1, brsvd+2*FATsize+ i, FAT);   // and the dictionary
  157.     bdos(0xd, 0, 0);                            // Flush I/O
  158.     delete FAT;
  159. }
  160.  
  161.